package com.yyds.controller;


import com.yyds.common.MyJSON;
import com.yyds.common.R;
import com.yyds.pojo.NewsNews;
import com.yyds.service.NewsNewsService;
import com.yyds.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/news/news")
@SuppressWarnings({"all"})//抑制警告
public class NewsNewsController {

    @Autowired
    NewsNewsService newsNewsService;
    public void setNewsNewsService(NewsNewsService newsNewsService) {
        this.newsNewsService = newsNewsService;
    }

    @RequestMapping("/list")
    public MyJSON<NewsNews> list(NewsNews newsNews, Integer page, Integer limit){
        return newsNewsService.selectAllNewsNewsService(newsNews,page, limit);
    }
    @RequestMapping("/add")
    public R add(NewsNews newsNews){
        return newsNewsService.insertNewsNewsService(newsNews);
    }

    @RequestMapping("/delAll")
    public R delAll(@RequestBody List<NewsNews> list){
        System.out.println("进入删除多条新闻 list= "+list);
        return newsNewsService.deleteAllNewsNewsService(list);
    }

    @RequestMapping("query")
    public R query(Integer news_id){
        return newsNewsService.selectNewsNewsService(news_id);
    }

    @RequestMapping("edit")
    public R edit(NewsNews newsNews){
        return newsNewsService.updateNewsNewsService(newsNews);
    }

    @RequestMapping("/load")
    public R load(MultipartFile file) throws IOException {
        String picUrl = OssUtil.uploadFile(file.getInputStream(), file.getOriginalFilename());
        return picUrl != null ? R.ok().data("url",picUrl) : R.error().data("url","图片找不到了,请重新上传");
    }

    @RequestMapping("/del")
    public R del(Integer news_id){
        System.out.println("进入删除单条新闻 news_id="+news_id);
        return newsNewsService.deleteNewsNewsService(news_id);
    }


    @RequestMapping("/pie")
    public R pie(){
        return newsNewsService.queryTypeNewsNewsService();
    }
}
